package socketing;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class V2Server {
//只支持同时一个用户的长连接
    public static void main2(String[] args) throws IOException, InterruptedException {

//        Scanner scanner = new Scanner(System.in);
//        while (true){
//
//            String s = scanner.nextLine();
//            if (s.equals("quit"))break;
//            Tool.debug(s);
//
//        }

        ServerSocket serverSocket = new ServerSocket(4444);
        while (true){

            Socket socket = serverSocket.accept();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(
                            socket.getOutputStream(), StandardCharsets.UTF_8));
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            socket.getInputStream(), StandardCharsets.UTF_8));
            System.out.println(socket.getPort()+"--socket from ->>"+socket.getLocalPort());
            while (true){
                String s = reader.readLine();
                if (s==null) break;
                Tool.debug(s);
                if (s.equals("half")) continue;
                Thread.sleep(3000);
                writer.write(s.toUpperCase()+"\r\n");
                writer.flush();
            }
            writer.close();
            reader.close();
            socket.close();

        }


    }
    private static final ExecutorService es
            = Executors.newCachedThreadPool();

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(4444);
        while (true){
            Socket socket = serverSocket.accept();
            System.out.println(socket.getPort() + "--socket is from ->>" + socket.getLocalPort());
            es.submit(new ServerShortTask(socket));
//            es.submit(new ServerTask(socket));


        }
    }


}
class ServerShortTask implements Runnable{
    private Socket socket;

    public ServerShortTask(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(
                            socket.getOutputStream(), StandardCharsets.UTF_8));
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            socket.getInputStream(), StandardCharsets.UTF_8));


                String s = reader.readLine();
                Tool.debug(s+"   sss");
                Thread.sleep(3000);
                writer.write(s.toUpperCase()+"\r\n");
                writer.flush();

            writer.close();
            reader.close();
            socket.close();


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
class ServerTask implements Runnable{
    private Socket socket;

    public ServerTask(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(
                            socket.getOutputStream(), StandardCharsets.UTF_8));
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            socket.getInputStream(), StandardCharsets.UTF_8));

            while (true){
                String s = reader.readLine();
                if (s==null) break;

                Tool.debug(s+"   sss");
                if (s.equals("half")) continue;
                Thread.sleep(3000);
                writer.write(s.toUpperCase()+"\r\n");
                writer.flush();
            }

            writer.close();
            reader.close();
            socket.close();


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
